<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"
	xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:pe="http://primefaces.org/ui/extensions"
	xmlns:f="http://java.sun.com/jsf/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions">

<ui:composition template="/pages/templates/crudTemplate.xhtml">

	<ui:define name="titulo">
		<h2>#{labels.menu_cadastro} / #{labels.menu_cadastro_usuario}</h2>
	</ui:define>

	<ui:define name="filterGrid">
	<h:panelGrid columns="3" width="100%" columnClasses=",,alignRight" id="filterGrid">
			<p:outputLabel for="nomeFilter" value="Nome" styleClass="label" />
			<p:outputLabel for="cpfFilter" value="CPF" styleClass="label" />
			<p:spacer />

			<p:inputText id="nomeFilter" styleClass="dados" style="width: 360px" value="#{usuarioController.filterEntity.nome}" />
			<p:inputMask id="cpfFilter" styleClass="dados" style="width: 120px" mask="999.999.999-99" value="#{usuarioController.filterEntity.cpf}"
				converter="maskConverter" />
			<p:commandButton value="Pesquisar" id="searchButton" icon="ui-icon-search" update="dtbEntities searchMessages"
				actionListener="#{usuarioController.search}" process="@this, filterGrid" oncomplete="dtb.getPaginator().setPage(0);" />
		</h:panelGrid>

	</ui:define>

	<ui:define name="resultTable">
	<p:dataTable id="dtbEntities" var="entity" emptyMessage="Nenhum usuário encontrado" value="#{usuarioController.entities}" paginator="true"
			paginatorPosition="bottom" rows="15" currentPageReportTemplate="{currentPage} de {totalPages}" paginatorAlwaysVisible="true"
			paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}" widgetVar="dtb">

			<p:column headerText="CPF" style="width: 110px;" styleClass="alignCenter" sortBy="formattedCpf">
				<h:outputText value="#{entity.formattedCpf}" />
			</p:column>

			<p:column headerText="Nome" sortBy="nome">
				<h:outputText value="#{entity.nome}" />
			</p:column>

			<p:column headerText="Papel" style="width: 140px;" styleClass="alignCenter" sortBy="dePapel">
				<h:outputText value="#{entity.dePapel}" />
			</p:column>

			<p:column style="width:40px" styleClass="alignCenter">
				<f:facet name="header">Ações</f:facet>
				<p:commandButton icon="ui-icon-pencil" title="Editar" oncomplete="editDialog.show();" update=":editForm" process="@this">
					<f:setPropertyActionListener value="#{entity}" target="#{usuarioController.selectedEntity}" />
					<pe:resetInput for=":editForm" />
				</p:commandButton>
				
			</p:column>

			<f:facet name="header">
				<p:commandButton value="Adicionar" icon="ui-icon-plus" actionListener="#{usuarioController.add}" oncomplete="editDialog.show();"
					update=":editForm" process="@this" global="false">
					<pe:resetInput for=":editForm" />
				</p:commandButton>
			</f:facet>

			<f:facet name="footer">
				<h:outputFormat value="#{fn:length(usuarioController.entities)} registro(s) encontrado(s)" />
			</f:facet>
		</p:dataTable>
	</ui:define>

<ui:define name="editDialog">
		<p:fieldset legend="Pessoal">
			<h:panelGrid columns="1" width="100%">

				<p:outputLabel for="cpf" value="CPF" styleClass="label" />

				<p:inputMask id="cpf" styleClass="dados" style="width: 120px" mask="999.999.999-99" value="#{usuarioController.entity.cpf}"
					converter="maskConverter" required="true" requiredMessage="Favor informar o CPF" validatorMessage="CPF inválido"
					readonly="#{usuarioController.editing}">
					<f:validator validatorId="checkvalidcpf" />
				</p:inputMask>

				<p:outputLabel for="nome" value="Nome" styleClass="label" />
				<p:inputText id="nome" styleClass="dados" style="width: 500px" required="true" requiredMessage="Favor informar o nome"
					value="#{usuarioController.entity.nome}" maxlength="100" />

				<p:outputLabel for="email" value="E-mail" styleClass="label" />
				<p:inputText id="email" styleClass="dados" style="width: 500px" required="true" requiredMessage="Favor informar o e-mail"
					validatorMessage="E-mail inválido" value="#{usuarioController.entity.email.valor}" maxlength="100">
					<f:validator validatorId="checkvalidemail" />
				</p:inputText>

			</h:panelGrid>
		</p:fieldset>

		<p:spacer />

		<p:fieldset legend="Acesso">
			<h:panelGrid columns="3" width="100%">
				<p:outputLabel for="status" value="Status" styleClass="label" />
				<p:outputLabel for="papel" value="Papel" styleClass="label" />
				<p:spacer />

				<p:selectOneMenu id="status" value="#{usuarioController.entity.status}" styleClass="dados" style="width:100px;" disabled="#{!usuarioController.editing}">
					<f:selectItems value="#{domPessoaController.statusPessoaFisicaMap}" />
				</p:selectOneMenu>
				<p:selectOneMenu id="papel" styleClass="dados" value="#{usuarioController.entity.codigoPapel}" style="width:160px;">
					<f:selectItems value="#{usuarioController.papeis}" />
				</p:selectOneMenu>
				<p:commandButton value="Gerar nova senha" icon="ui-icon-mail-closed" update=":editForm:editPanel"
					actionListener="#{usuarioController.resetPassword}" disabled="#{!usuarioController.editing}" />
			</h:panelGrid>
		</p:fieldset>
	</ui:define>

	<ui:define name="buttons">
		<p:commandButton id="submitButton" value="Confirmar" icon="ui-icon-check" actionListener="#{usuarioController.save}" process="editPanel"
			update=":editForm:editPanel :searchForm:dtbEntities" />
		<p:commandButton value="Fechar" icon="ui-icon-close" oncomplete="editDialog.hide();" process="@none" global="false" />
	</ui:define>

	<ui:define name="complement">
	</ui:define>
</ui:composition>
</html>